package com.ophiux.auth.service;

import com.ophiux.common.core.base.Result;
import com.ophiux.common.core.constant.SecurityConstants;
import com.ophiux.common.security.service.OphiuxUser;
import com.ophiux.common.security.service.OphiuxUserDetailsService;
import com.ophiux.system.api.feign.RemoteSocialDetailsService;
import com.ophiux.system.model.dto.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

/**
 * @desc: 手机号验证码登录
 * @author: hhl
 * @date：2023/12/9 20:39
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OphiuxMobileUserDetailServiceImpl implements OphiuxUserDetailsService {

    private final UserDetailsService userDetailsService;

    private final RemoteSocialDetailsService remoteSocialDetailsService;

    @Override
    public boolean support(String clientId, String grantType) {
        log.info("grantType={},clientId={}", grantType, clientId);
        return SecurityConstants.GRANT_MOBILE.equals(grantType);
    }

    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String phone) {
        Result<UserInfo> result = remoteSocialDetailsService.social(phone, SecurityConstants.FROM_IN);
        return getUserDetails(result);
    }

    @Override
    public UserDetails loadUserByUser(OphiuxUser ophiuxUser) {
        return userDetailsService.loadUserByUsername(ophiuxUser.getUsername());
    }

}
